<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\Relation;
use app\common\model\Postulant;
use app\common\model\Expert;
use app\common\model\UserRole;
use app\common\model\Level;
use app\common\tool\Result;
use app\common\model\User;
use think\exception\ValidateException;

class Team extends Base
{

    /**
     * 申请服务/专家团队
     */
    public function expertTeamAdd()
    {
        try {
            $param = $this->request->post();
            $this->validate($param, 'Expert');
            if (!in_array($param['type'], [3, 4])) {
                return Result::fail('申请团队类型错误');
            }
            return Result::fastReturn(Expert::add($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        } catch (ValidateException $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取申请资料
     */
    public function getTeamDetails()
    {
        try {
            $param = $this->request->get();
            $map[] = ['uid', '=', $this->user->id];
            if (!in_array($param['type'], [2, 3, 4])) {
                return Result::fail('申请团队类型错误');
            }
            $map[] = ['type', '=', $param['type']];
            $info = Expert::where($map)->find();
            if (empty($info)) {
                return Result::success('数据不存在！',['status'=>0]);
            }
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取职称
     */
    public function getJobTitle($type)
    {
        try {
            if (!in_array($type, [3, 4])) {
                return Result::fail('团队类型错误');
            }
            $list = UserRole::where('type', $type)->field('id,title')->select();
            return Result::success('获取成功！', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取首页专家团队
     */
    public function getExpertList()
    {
        try {
            $list = Level::where('id', '>', 2)->field('id,title')->select();
            $uid = User::where('is_lock', 0)->column('id');
            foreach ($list as &$v) {
                $v->children = Expert::where(['status' => 2, 'type' => $v['id']])->whereIn('uid', $uid)->field('id,rname,icon,job')->limit(6)->select();
            }
            return Result::success('获取成功', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取列表
     */
    public function getList($type, $page)
    {
        try {
            $uid = User::where('is_lock', 0)->column('id');
            $map[] = ['status', '=', 2];
            $map[] = ['type', '=', $type];
            $map[] = ['uid', 'in', $uid];
            $list = Expert::getPageList($map, 'id,rname,icon,uid,job,speciality', $page, 20, false);
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取专家团队详情
     */
    public function getExpertDetails($id)
    {
        try {
            $info = Expert::where(['id' => $id, 'status' => 2])->field('id,rname,icon,job,speciality,images,note')->find();
            if (empty($info)) {
                return Result::fail('数据不存在！');
            }
            $info->images = !empty($info->images) ? explode(',', $info->images) : [];
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
